package cn.itcast.crawler.test;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class ClawlerFirst {

    public static void main(String[] args) throws IOException {
     //1.打开浏览器,创建HttpClients对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        //2.输入网址,发起get请求,创建httpget对象
        HttpGet httpGet = new HttpGet("http://www.itcast.cn/");
        //3.发起请求,返回响应,使用HttpClient发起对象
        CloseableHttpResponse execute = httpclient.execute(httpGet);
        //4.解析响应,获取数据
        //判断状态码是否200
        if(execute.getStatusLine().getStatusCode()==200){
            HttpEntity entity = execute.getEntity();
            String utf8 = EntityUtils.toString(entity, "utf8");
            System.out.println(utf8);
        }

    }
}
